package userManagement;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import db.DatabaseConnector;

public class AddUser extends HttpServlet {
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
	PrintWriter out = response.getWriter();

        HttpSession session = request.getSession(true);
	DatabaseConnector DBConnect = new DatabaseConnector(DatabaseConnector.getDatabaseType());

        try
        {
            //available values are 'add' and 'existent'
            boolean exists = request.getParameter("loadCase").equals("existent");

            out.println("<html><head><title>User Addition</title>");
            out.println("<script type=\"text/javascript\" src=\"../userManager.js\"></script></head><body>");

            if(exists){
		out.println("<h3><font color=\"red\">Username '"+ request.getParameter("username")
		 	  + "' already exists!</font></h3>");
            }
            out.println("<h2 id=\"pageTitle\">Add New User</h2><!-- NOTE: used to ID process -->");
            out.println("<table>");

            if (exists){ //read data from URL
                out.println("<tr><td>Username:</td><td><input type=\"text\" id=\"username\" style=\"width:250px\" value=\""
        		    + request.getParameter("username") + "\" /></td></tr>");
                out.println("<tr><td>Password:</td><td><input type=\"password\" id=\"password1\" style=\"width:250px\" value=\""
        		    + request.getParameter("password") + "\" /></td></tr>");
                out.println("<tr><td>Re-type Password:</td><td><input type=\"password\" id=\"password2\" style=\"width:250px\" value=\""
                            + request.getParameter("password") + "\" /></td></tr>");
                out.println("<tr><td>E-mail:</td><td><input type=\"text\" id=\"email\" style=\"width:250px\" value=\""
                            + request.getParameter("email") + "\" /></td></tr>");
            } else {
                out.println("<tr><td>Username:</td><td><input type=\"text\" id=\"username\" style=\"width:250px\" /></td></tr>");
                out.println("<tr><td>Password:</td><td><input type=\"password\" id=\"password1\" style=\"width:250px\" /></td></tr>");
                out.println("<tr><td>Re-type Password:</td><td><input type=\"password\" id=\"password2\" style=\"width:250px\" /></td></tr>");
                out.println("<tr><td>E-mail:</td><td><input type=\"text\" id=\"email\" style=\"width:250px\" /></td></tr>");
            } out.println("</table><br />");

            //group stuffff
            out.print("<b>Groups</b><ul id=\"grouplist\">");

            String[] groups = {"chat"};
            if (exists && !request.getParameter("groups").equals("")){
                groups = request.getParameter("groups").split(";");
                for (int i=0;i<groups.length;i++){
                    out.println("<li>" + groups[i] + "</li>");
                }
            } else out.print("<li>" + groups[0] + "</li>");
                out.println("</ul>");

                out.println("Add User to Group:<br />");
                out.println("<select id=\"availList\" style=\"width:150px\">");
                out.println("<option>Select Group...</option>");

                String[][] rs = DBConnect.selectSQL("ROLES",new String[] {"*"});
                //if (exists && !request.getParameter("groups").equals("")) {
                    for (int i=0;i<rs.length;i++) {
                        int l=0;
                        for (int j=0;j<groups.length;j++,l++){
                            if (rs[i][0].equals(groups[j])) break; }
                        if (l<groups.length) continue;
                            out.println("<option>"+rs[i][0]+"</option>");
                    }
                //} else {
                 //   for (int i=0;i<rs.length;i++) {
                //        out.println("<option>"+rs[i][0]+"</option>"); }
                //}
                out.println("</select> <input type=\"button\" value=\"Add\" onclick=\"addGroup()\" /><br /><br />");

                out.println("Remove User from Group:<br />");
                out.println("<select size=\"1\" id=\"removeList\" style=\"width:150px\">");
                out.println("<option>Select Group...</option>");
                //if (exists && !request.getParameter("groups").equals("")) {
                    for (int i=0;i<groups.length;i++){
                        out.println("<option>"+ groups[i] +"</option>"); }
                //}
                out.println("</select>");
                out.println("<input type=\"button\" value=\"Remove\" onclick=\"remGroup()\" />");

                out.println("<hr width=\"400px\" align=\"left\" />");
                out.println("<div style=\"width:400px\"><center>");
                out.println("<form method=\"post\" action=\"ListUsers\">");
                out.println("<input type=\"button\" value=\"Add User\" onclick=\"submitUser()\" style=\"font:bold 14px Times New Roman\" />");
                out.println("<input type=\"submit\" value=\"Return to User List\" /></form></center></div>");

                out.println("</body></html>");
                out.close();
            }
            finally
            {
                DBConnect.CleanUp();
            }
        }

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
			doPost(request, response);
	}
}